<template>
	<xn-form-container
		:title="formData.id ? '编辑承运人' : '增加承运人'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="承运人编码：" name="carrierCode">
						<a-input v-model:value="formData.carrierCode" placeholder="请输入承运人编码" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="承运人名称：" name="carrierName">
						<a-input v-model:value="formData.carrierName" placeholder="请输入承运人名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="负责人：" name="carrierPerson">
						<a-input v-model:value="formData.carrierPerson" placeholder="请输入负责人" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="联系人：" name="carrierContact">
						<a-input v-model:value="formData.carrierContact" placeholder="请输入联系人" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="联系电话：" name="carrierTel">
						<a-input v-model:value="formData.carrierTel" placeholder="请输入联系电话" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="邮箱：" name="carrierEmail">
						<a-input v-model:value="formData.carrierEmail" placeholder="请输入邮箱" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="传真：" name="carrierFax">
						<a-input v-model:value="formData.carrierFax" placeholder="请输入传真" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="开户行：" name="carrierBank">
						<a-input v-model:value="formData.carrierBank" placeholder="请输入开户行" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="银行账号：" name="carrierBankcode">
						<a-input v-model:value="formData.carrierBankcode" placeholder="请输入银行账号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="地址：" name="carrierAddress">
						<a-input v-model:value="formData.carrierAddress" placeholder="请输入地址" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="应付运费：" name="carrierAccountsPayable">
						<a-input v-model:value="formData.carrierAccountsPayable" placeholder="请输入应付运费" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="备注：" name="carrierRemark">
						<a-input v-model:value="formData.carrierRemark" placeholder="请输入备注" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="24">
					<a-form-item label="附件：" name="carrierAttachment">
						<xn-upload v-model:value="formData.carrierAttachment" :uploadNumber=10 uploadMode="image" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="supplierCarrierForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import supplierCarrierApi from '@/api/supplier/base/supplierCarrierApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		carrierCode: [required('请输入承运人编码')],
		carrierName: [required('请输入承运人名称')],
		carrierPerson: [required('请输入负责人')],
		carrierContact: [required('请输入联系人')],
		carrierTel: [required('请输入联系电话')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				supplierCarrierApi
					.supplierCarrierSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
